package com.mhc.suzuki.controller;

import com.alibaba.fastjson.JSON;
import com.mhc.haval.security.model.User;
import com.mhc.suzuki.bo.GpsInfoBO;
import com.mhc.suzuki.bo.SubmitTaskBO;
import com.mhc.suzuki.constant.ErrorCodeEnum;
import com.mhc.suzuki.exception.SuzukiCommonException;
import com.mhc.suzuki.form.CollectDataForm;
import com.mhc.suzuki.form.SubmitTaskForm;
import com.mhc.suzuki.service.ApplyService;
import com.mhc.suzuki.service.SubmitTaskService;
import com.mhc.suzuki.vo.WanderResultVO;
import com.subaru.common.entity.BizResult;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.annotation.AuthenticationPrincipal;
import org.springframework.web.bind.annotation.*;

/**
 * 各角色提交方法
 * Created by HMJ on 2017/9/14.
 */
@Slf4j
@RestController
@RequestMapping(value = "/submitTaskController")     // 通过这里配置使下面的映射都在/submitTaskController，可去除
public class SubmitTaskController {

    @Autowired
    private SubmitTaskService submitTaskService;
    @Autowired
    private ApplyService applyService;

    /**
     * 审核员提交审核
     * @param submitTaskForm
     * @param logedUser
     * @return
     */
    @ApiOperation(value = "审核员提交审核", httpMethod = "POST",notes = "审核员提交审核")
    @RequestMapping(value = {"/auditorSubmitTask"}, method = RequestMethod.POST)
    @ApiImplicitParam(name = "submitTaskForm", value = "提交审核信息", required = false,paramType = "body",dataType = "SubmitTaskForm")
    public BizResult auditorSubmitTask(@RequestBody SubmitTaskForm submitTaskForm, @AuthenticationPrincipal User logedUser) {
        try{
            SubmitTaskBO submitTaskBO = new SubmitTaskBO();
            BeanUtils.copyProperties(submitTaskForm,submitTaskBO);
            boolean submitTaskFlag = submitTaskService.auditorSubmitTask(submitTaskBO,logedUser);
            log.info("审核员提交审核成功...");
            return BizResult.create(submitTaskFlag,ErrorCodeEnum.SUCCESS.getErrCode(),ErrorCodeEnum.SUCCESS.getErrMsg());
        }catch (SuzukiCommonException e){
            log.error("auditorSubmitTask SuzukiCommonException,订单号："+submitTaskForm.getOrderNo(),e);
            return BizResult.create(e.getCode(),e.getMessage());
        }
    }


    /**
     * 业务员撤销申请
     * @param submitTaskForm
     * @param logedUser
     * @return
     */
    @ApiOperation(value = "业务员撤销申请", httpMethod = "POST",notes = "业务员撤销申请")
    @RequestMapping(value = {"/salesManRevokeTask"}, method = RequestMethod.POST)
    @ApiImplicitParam(name = "submitTaskForm", value = "提交审核信息", required = false,paramType = "body",dataType = "SubmitTaskForm")
    public BizResult salesManRevokeTask(@RequestBody SubmitTaskForm submitTaskForm, @AuthenticationPrincipal User logedUser) {
        try{
            SubmitTaskBO submitTaskBO = new SubmitTaskBO();
            BeanUtils.copyProperties(submitTaskForm,submitTaskBO);
            boolean submitTaskFlag = submitTaskService.salesManRevokeTask(submitTaskBO,logedUser);
            log.info("业务员撤销申请成功...");
            return BizResult.create(submitTaskFlag,ErrorCodeEnum.SUCCESS.getErrCode(),ErrorCodeEnum.SUCCESS.getErrMsg());

        }catch (SuzukiCommonException e){
            log.error("salesManRevokeTask SuzukiCommonException,订单号："+submitTaskForm.getOrderNo(),e);
            return BizResult.create(e.getCode(),e.getMessage());
        }
    }

    /**
     * 业务员作废申请
     * @param submitTaskForm
     * @param logedUser
     * @return
     */
    @ApiOperation(value = "业务员作废申请", httpMethod = "POST",notes = "业务员作废申请")
    @RequestMapping(value = {"/invalidApply"}, method = RequestMethod.POST)
    @ApiImplicitParam(name = "submitTaskForm", value = "提交审核信息", required = false,paramType = "body",dataType = "SubmitTaskForm")
    public BizResult invalidApply(@RequestBody SubmitTaskForm submitTaskForm, @AuthenticationPrincipal User logedUser) {
        try{
            SubmitTaskBO submitTaskBO = new SubmitTaskBO();
            BeanUtils.copyProperties(submitTaskForm,submitTaskBO);
            boolean submitTaskFlag = submitTaskService.invalidApply(submitTaskBO,logedUser);
            log.info("业务员作废申请成功...");
            return BizResult.create(submitTaskFlag,ErrorCodeEnum.SUCCESS.getErrCode(),ErrorCodeEnum.SUCCESS.getErrMsg());
        }catch (SuzukiCommonException e){
            log.error("invalidApply SuzukiCommonException,订单号："+submitTaskForm.getOrderNo(),e);
            return BizResult.create(e.getCode(),e.getMessage());
        }
    }


    /**
     * 业务员发起贷款申请
     * @param submitTaskForm
     * @param logedUser
     * @return
     */
    @ApiOperation(value = "业务员提交贷款申请", httpMethod = "POST",notes = "业务员提交贷款申请")
    @RequestMapping(value = {"/salesManLoanApply"}, method = RequestMethod.POST)
    @ApiImplicitParam(name = "submitTaskForm", value = "提交审核信息", required = false,paramType = "body",dataType = "SubmitTaskForm")
    public BizResult salesManLoanApply(@RequestBody SubmitTaskForm submitTaskForm, @AuthenticationPrincipal User logedUser) {
        try{
            SubmitTaskBO submitTaskBO = new SubmitTaskBO();
            BeanUtils.copyProperties(submitTaskForm,submitTaskBO);
            Boolean submitTaskFlag = submitTaskService.salesManLoanApply(submitTaskBO,logedUser);
            log.info("业务员发起贷款申请成功...");
            return BizResult.create(submitTaskFlag,ErrorCodeEnum.SUCCESS.getErrCode(),ErrorCodeEnum.SUCCESS.getErrMsg());
        }catch (SuzukiCommonException e){
            log.error("salesManLoanApply SuzukiCommonException,订单号："+submitTaskForm.getOrderNo(),e);
            return BizResult.create(e.getCode(),e.getMessage());
        }
    }


    /**
     * 业务员发起打款申请
     * @param submitTaskForm
     * @param logedUser
     * @return
     */
    @ApiOperation(value = "业务员发起打款申请", httpMethod = "POST",notes = "业务员发起打款申请")
    @RequestMapping(value = {"/salesManPayMentApply"}, method = RequestMethod.POST)
    @ApiImplicitParam(name = "submitTaskForm", value = "提交审核信息", required = false,paramType = "body",dataType = "SubmitTaskForm")
    public BizResult salesManPayMentApply(@RequestBody SubmitTaskForm submitTaskForm, @AuthenticationPrincipal User logedUser) {
        try{
            SubmitTaskBO submitTaskBO = new SubmitTaskBO();
            BeanUtils.copyProperties(submitTaskForm,submitTaskBO);
            WanderResultVO wanderResultVO = submitTaskService.salesManPayMentApply(submitTaskBO,logedUser);
            if(wanderResultVO.getIsPayMentApply()!=null &&wanderResultVO.getIsPayMentApply()){
                log.info("业务员发起打款申请成功...");
                return BizResult.create(wanderResultVO.getIsPayMentApply(),ErrorCodeEnum.SUCCESS.getErrCode(),ErrorCodeEnum.SUCCESS.getErrMsg());
            }else {
                log.info("处于飘红...");
                return BizResult.create(wanderResultVO.getCollectWanderList(),ErrorCodeEnum.WANDERRULERERROR.getErrCode(),ErrorCodeEnum.WANDERRULERERROR.getErrMsg());
            }
        }catch (SuzukiCommonException e){
            log.error("salesManPayMentApply SuzukiCommonException,订单号："+submitTaskForm.getOrderNo(),e);
            return BizResult.create(e.getCode(),e.getMessage());
        }
    }


    /**
     * 业务员补录资料
     * @param collectDataForm
     * @param logedUser
     * @return
     */
    @ApiOperation(value = "业务员补录资料", httpMethod = "POST",notes = "业务员补录资料")
    @RequestMapping(value = {"/collectionData"}, method = RequestMethod.POST)
    @ApiImplicitParam(name = "collectDataForm", value = "补录资料信息", required = false,paramType = "body",dataType = "CollectDataForm")
    public BizResult collectionData(@RequestBody CollectDataForm collectDataForm, @AuthenticationPrincipal User logedUser) {
        try{
            /**获取GPS信息**/
            GpsInfoBO gpsInfoBO = new GpsInfoBO();      //GPS信息
            BeanUtils.copyProperties(collectDataForm,gpsInfoBO);
            submitTaskService.updateGPSInfo(gpsInfoBO,logedUser);
            log.info("打款申请补录资料成功");
            return BizResult.create(null,ErrorCodeEnum.SUCCESS.getErrCode(),ErrorCodeEnum.SUCCESS.getErrMsg());
        }catch (SuzukiCommonException e){
            log.error("collectionData SuzukiCommonException,订单号："+collectDataForm.getOrderNo(),e);
            return BizResult.create(e.getCode(),e.getMessage());
        }
    }

    /**
     * 查询补录资料信息
     * @param orderNo
     * @return
     */
    @ApiOperation(value = "查询补录资料信息", httpMethod = "POST",notes = "查询补录资料信息")
    @RequestMapping(value = {"/selectCollectionData/{orderNo}"}, method = RequestMethod.GET)
    @ApiImplicitParam(name = "orderNo", value = "订单号", required = true, paramType = "path", dataType = "String")
    public BizResult selectCollectionData(@PathVariable String orderNo) {
        try{
            GpsInfoBO gpsInfoBO = submitTaskService.selectGPSInfo(orderNo);
            log.info("查询补录资料成功");
            return BizResult.create(gpsInfoBO,ErrorCodeEnum.SUCCESS.getErrCode(),ErrorCodeEnum.SUCCESS.getErrMsg());
        }catch (SuzukiCommonException e){
            log.error("selectCollectionData SuzukiCommonException,订单号："+orderNo,e);
            return BizResult.create(e.getCode(),e.getMessage());
        }
    }

    /**
     * 业务员发起通融申请
     * @param submitTaskForm
     * @param logedUser
     * @return
     */
    @ApiOperation(value = "业务员发起通融申请", httpMethod = "POST",notes = "业务员发起通融申请")
    @RequestMapping(value = {"/accomApply"}, method = RequestMethod.POST)
    @ApiImplicitParam(name = "submitTaskForm", value = "提交审核信息", required = false,paramType = "body",dataType = "SubmitTaskForm")
    public BizResult accomApply(@RequestBody SubmitTaskForm submitTaskForm, @AuthenticationPrincipal User logedUser) {
        try{
            SubmitTaskBO submitTaskBO = new SubmitTaskBO();
            BeanUtils.copyProperties(submitTaskForm,submitTaskBO);
            boolean submitTaskFlag = submitTaskService.accomApply(submitTaskBO,logedUser);
            log.info("业务员发起通融申请成功...");
            return BizResult.create(submitTaskFlag,ErrorCodeEnum.SUCCESS.getErrCode(),ErrorCodeEnum.SUCCESS.getErrMsg());

        }catch (SuzukiCommonException e){
            log.error("accomApply SuzukiCommonException,订单号："+submitTaskForm.getOrderNo(),e);
            return BizResult.create(e.getCode(),e.getMessage());
        }
    }

    /**
     * 业务员发起修改申请
     * @param submitTaskForm
     * @param logedUser
     * @return
     */
    @ApiOperation(value = "业务员发起修改申请", httpMethod = "POST",notes = "业务员发起修改申请")
    @RequestMapping(value = {"/updateApply"}, method = RequestMethod.POST)
    @ApiImplicitParam(name = "submitTaskForm", value = "提交审核信息", required = false,paramType = "body",dataType = "SubmitTaskForm")
    public BizResult updateApply(@RequestBody SubmitTaskForm submitTaskForm, @AuthenticationPrincipal User logedUser) {
        try{
            SubmitTaskBO submitTaskBO = new SubmitTaskBO();
            BeanUtils.copyProperties(submitTaskForm,submitTaskBO);
            log.info("业务员发起修改申请开始...");
            boolean submitTaskFlag = submitTaskService.updateApply(submitTaskBO,logedUser);
            log.info("业务员发起修改申请成功...");
            return BizResult.create(submitTaskFlag,ErrorCodeEnum.SUCCESS.getErrCode(),ErrorCodeEnum.SUCCESS.getErrMsg());
        }catch (SuzukiCommonException e){
            log.error("updateApply SuzukiCommonException,订单号："+submitTaskForm.getOrderNo(),e);
            return BizResult.create(e.getCode(),e.getMessage());
        }
    }


    /**
     * 资产保全业务员、内勤提交审核
     * @param submitTaskForm
     * @param logedUser
     * @return
     */
    @ApiOperation(value = "资产保全业务员、内勤提交审核", httpMethod = "POST",notes = "资产保全业务员、内勤提交审核")
    @RequestMapping(value = {"/assetsSubmitTask"}, method = RequestMethod.POST)
    @ApiImplicitParam(name = "submitTaskForm", value = "提交审核信息", required = false,paramType = "body",dataType = "SubmitTaskForm")
    public BizResult assetsSubmitTask(@RequestBody SubmitTaskForm submitTaskForm, @AuthenticationPrincipal User logedUser) {
        try{
            log.info("提交审核开始...", JSON.toJSONString(submitTaskForm));
            if(submitTaskForm.getApplyType()==null){
                return BizResult.create(ErrorCodeEnum.APPLY_TYPE_ISNULL.getErrCode(),ErrorCodeEnum.APPLY_TYPE_ISNULL.getErrMsg());
            }
            SubmitTaskBO submitTaskBO = new SubmitTaskBO();
            BeanUtils.copyProperties(submitTaskForm,submitTaskBO);
            boolean submitTaskFlag = submitTaskService.assetsSubmitTask(submitTaskBO,logedUser);
            log.info("提交审核成功...");
            return BizResult.create(submitTaskFlag,ErrorCodeEnum.SUCCESS.getErrCode(),ErrorCodeEnum.SUCCESS.getErrMsg());
        }catch (SuzukiCommonException e){
            log.error("assetsSubmitTask SuzukiCommonException,订单号："+submitTaskForm.getOrderNo(),e);
            return BizResult.create(e.getCode(),e.getMessage());
        }
    }


    /**
     * 检查征信已录入后是否可以发起通融申请按钮
     * @param orderNo
     * @return
     */
    @ApiOperation(value = "检查征信已录入后是否可以发起通融申请按钮", httpMethod = "GET",notes = "检查征信已录入后是否可以发起通融申请按钮")
    @RequestMapping(value = {"/isAllowToAccomApply/{orderNo}"}, method = RequestMethod.GET)
    @ApiImplicitParam(name = "orderNo", value = "订单号", required = true, paramType = "path", dataType = "String")
    public BizResult isAllowToAccomApply(@PathVariable String orderNo) {
        try{
            boolean submitTaskFlag = submitTaskService.isShowAccomApply(orderNo);
            log.info("检查成功...");
            return BizResult.create(submitTaskFlag,ErrorCodeEnum.SUCCESS.getErrCode(),ErrorCodeEnum.SUCCESS.getErrMsg());
        }catch (SuzukiCommonException e){
            log.error("isAllowToAccomApply SuzukiCommonException,订单号："+orderNo,e);
            return BizResult.create(e.getCode(),e.getMessage());
        }
    }


    /**
     * 保存GPS确认字段
     * @param submitTaskForm
     * @param logedUser
     * @return
     */
    @ApiOperation(value = "保存GPS确认字段", httpMethod = "POST",notes = "保存GPS确认字段")
    @RequestMapping(value = {"/saveGPSInfo"}, method = RequestMethod.POST)
    @ApiImplicitParam(name = "submitTaskForm", value = "GPS信息", required = false,paramType = "body",dataType = "SubmitTaskForm")
    public BizResult saveGPSInfo(@RequestBody SubmitTaskForm submitTaskForm, @AuthenticationPrincipal User logedUser) {
        try{
            SubmitTaskBO submitTaskBO = new SubmitTaskBO();
            BeanUtils.copyProperties(submitTaskForm,submitTaskBO);
            boolean saveFlag = submitTaskService.saveGPSInfo(submitTaskBO,logedUser);
            if(saveFlag){
                log.info("保存GPS信息成功...");
                return BizResult.create(saveFlag,ErrorCodeEnum.SUCCESS.getErrCode(),ErrorCodeEnum.SUCCESS.getErrMsg());
            }else {
                log.error("GPS信息缺失...");
                return BizResult.create(saveFlag,ErrorCodeEnum.FAILURE.getErrCode(),ErrorCodeEnum.FAILURE.getErrMsg());
            }
        }catch (SuzukiCommonException e){
            log.error("saveGPSInfo SuzukiCommonException,订单号："+submitTaskForm.getOrderNo(),e);
            return BizResult.create(e.getCode(),e.getMessage());
        }
    }


    /**
     * 是否显示GPS确认信息
     * @param orderNo
     * @param logedUser
     * @return
     */
    @ApiOperation(value = "是否显示GPS确认信息", httpMethod = "GET",notes = "是否显示GPS确认信息")
    @RequestMapping(value = {"/isShowGPS/{orderNo}"}, method = RequestMethod.GET)
    @ApiImplicitParam(name = "orderNo", value = "订单号", required = true, paramType = "path", dataType = "String")
    public BizResult isShowGPS(@PathVariable String orderNo, @AuthenticationPrincipal User logedUser) {
        try{
            boolean submitTaskFlag = submitTaskService.isShowGPS(orderNo,logedUser);
            log.info("检查成功...");
            return BizResult.create(submitTaskFlag,ErrorCodeEnum.SUCCESS.getErrCode(),ErrorCodeEnum.SUCCESS.getErrMsg());
        }catch (SuzukiCommonException e){
            log.error("isShowGPS SuzukiCommonException,订单号："+orderNo,e);
            return BizResult.create(e.getCode(),e.getMessage());
        }
    }
}
